export default function(coder: any) {


  coder['control_repeat'] = function(block: any) {
    // debugger
    let repeats = coder.valueToCode(block, 'TIMES', coder.ORDER_ASSIGNMENT) || '0'
    let tmp = coder.defineTmpVar() ;
    let code = `${coder.indentString()}for (var ${tmp}=0; ${tmp}<${repeats}; ${tmp}++){\n`
    coder.indent++
    code+= coder.statementToCode(block, 'SUBSTACK');
    coder.indent--
    code+= `${coder.indentString()}}\n`;
    return code;
  };

  coder['control_repeat_until'] = function(block: any) {
    let conditionCode = coder.valueToCode(block, 'CONDITION',coder.ORDER_NONE) || 'false';
    let code = `${coder.indentString()}while( !( ${conditionCode} ) ) {\n`
    coder.indent ++
    code+= coder.statementToCode(block, 'SUBSTACK');
    coder.indent --
    code+= `${coder.indentString()}}`
    return code
  };

  coder['control_for'] = function(block: any) {
    // For loop.
    var variable0 = coder.variableDB_.getName(
        block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
    var argument0 = coder.valueToCode(block, 'FROM',
        coder.ORDER_ASSIGNMENT) || '0';
    var argument1 = coder.valueToCode(block, 'TO',
        coder.ORDER_ASSIGNMENT) || '0';
    var increment = coder.valueToCode(block, 'BY',
        coder.ORDER_ASSIGNMENT) || '1';
    var branch = coder.statementToCode(block, 'DO');
    branch = coder.addLoopTrap(branch, block.id);
    var code;
    if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
        Blockly.isNumber(increment)) {
      // All arguments are simple numbers.
      var up = parseFloat(argument0) <= parseFloat(argument1);
      code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
          variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
          variable0;
      var step = Math.abs(parseFloat(increment));
      if (step == 1) {
        code += up ? '++' : '--';
      } else {
        code += (up ? ' += ' : ' -= ') + step;
      }
      code += ') {\n' + branch + '}\n';
    } else {
      code = '';
      // Cache non-trivial values to variables to prevent repeated look-ups.
      var startVar = argument0;
      if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
        startVar = coder.variableDB_.getDistinctName(
            variable0 + '_start', Blockly.Variables.NAME_TYPE);
        code += 'var ' + startVar + ' = ' + argument0 + ';\n';
      }
      var endVar = argument1;
      if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
        var endVar = coder.variableDB_.getDistinctName(
            variable0 + '_end', Blockly.Variables.NAME_TYPE);
        code += 'var ' + endVar + ' = ' + argument1 + ';\n';
      }
      // Determine loop direction at start, in case one of the bounds
      // changes during loop execution.
      var incVar = coder.variableDB_.getDistinctName(
          variable0 + '_inc', Blockly.Variables.NAME_TYPE);
      code += 'var ' + incVar + ' = ';
      if (Blockly.isNumber(increment)) {
        code += Math.abs(increment) + ';\n';
      } else {
        code += 'Math.abs(' + increment + ');\n';
      }
      code += 'if (' + startVar + ' > ' + endVar + ') {\n';
      code += coder.INDENT + incVar + ' = -' + incVar + ';\n';
      code += '}\n';
      code += 'for (' + variable0 + ' = ' + startVar + '; ' +
          incVar + ' >= 0 ? ' +
          variable0 + ' <= ' + endVar + ' : ' +
          variable0 + ' >= ' + endVar + '; ' +
          variable0 + ' += ' + incVar + ') {\n' +
          branch + '}\n';
    }
    return code;
  };

  coder['control_forEach'] = function(block: any) {
    // For each loop.
    var variable0 = coder.variableDB_.getName(
        block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
    var argument0 = coder.valueToCode(block, 'LIST',
        coder.ORDER_ASSIGNMENT) || '[]';
    var branch = coder.statementToCode(block, 'DO');
    branch = coder.addLoopTrap(branch, block.id);
    var code = '';
    // Cache non-trivial values to variables to prevent repeated look-ups.
    var listVar = argument0;
    if (!argument0.match(/^\w+$/)) {
      listVar = coder.variableDB_.getDistinctName(
          variable0 + '_list', Blockly.Variables.NAME_TYPE);
      code += 'var ' + listVar + ' = ' + argument0 + ';\n';
    }
    var indexVar = coder.variableDB_.getDistinctName(
        variable0 + '_index', Blockly.Variables.NAME_TYPE);
    branch = coder.INDENT + variable0 + ' = ' +
        listVar + '[' + indexVar + '];\n' + branch;
    code += 'for (var ' + indexVar + ' in ' + listVar + ') {\n' + branch + '}\n';
    return code;
  };

  coder['control_flow_statements'] = function(block: any) {
    // Flow statements: continue, break.
    switch (block.getFieldValue('FLOW')) {
      case 'BREAK':
        return 'break;\n';
      case 'CONTINUE':
        return 'continue;\n';
    }
    throw Error('Unknown flow statement.');
  };
}